import java.util.ArrayList;
import java.util.Scanner;

public class StudentManger {
    public static void main(String[] args) {
        ArrayList<Student1> array = new ArrayList<Student1>();
        while (true){
        System.out.println("------欢迎来到学生管理系统------");
        System.out.println("1 添加学生");
        System.out.println("2 删除学生");
        System.out.println("3 修改学生");
        System.out.println("4 查看所有学生");
        System.out.println("5 退出");
        System.out.println("请输入你的选择:");
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        switch (s){
            case "1":
//                System.out.println("添加学生");
                addStudent(array);
                break;
            case "2":
//                System.out.println("删除学生");
                deleteStudent(array);
                break;
            case "3":
//                System.out.println("修改学生");

                break;
            case "4":
//                System.out.println("查看所有学生");
                findStudent(array);
                break;
            case "5":
                System.out.println("谢谢使用");
                System.exit(0);
        }
       }
    }

    public static void addStudent(ArrayList<Student1> array){
        Scanner scanner = new Scanner(System.in);
        String sid;
        while (true){
        System.out.println("请输入学生学号");
        sid = scanner.nextLine();
        boolean flag = isUsed(array, sid);
        if (flag)
        {
            System.out.println("你输入的学号已经存在，请重新输入");
        }else
                break;
        }
        System.out.println("请输入学生姓名");
        String name = scanner.nextLine();
        System.out.println("请输入学生年龄");
        String age = scanner.nextLine();
        System.out.println("请输入学生住址");
        String address = scanner.nextLine();

        Student1 s1 = new Student1();
        s1.setSid(sid);
        s1.setName(name);
        s1.setAge(age);
        s1.setAddress(address);

        array.add(s1);
        System.out.println("添加学生成功");
    }
    public  static  boolean isUsed(ArrayList<Student1> array,String sid){
        boolean flag=false;
        for (int i=0;i<array.size();i++){
            Student1 student1 = array.get(i);
            if (student1.getSid().equals(sid)){
                flag=true;
                break;
            }
        }
        return flag;
    }
    public  static void findStudent(ArrayList<Student1> array){
       if (array.size()==0){
           System.out.println("无信息，请先添加学生信息");
       }
       else
        {
        System.out.println("学号\t\t\t姓名\t\t年龄\t\t居住地");
        for (int i =0;i<array.size();i++){
            Student1 s = array.get(i);
            System.out.println(s.getSid()+"\t\t\t"+s.getName()+"\t\t"+s.getAge()+"岁\t\t"+s.getAddress());
        }
        }
    }

    public  static void deleteStudent(ArrayList<Student1> array){
        System.out.println("请输入你要删除学生的学号：");
        Scanner scanner = new Scanner(System.in);
        String sid = scanner.nextLine();
        if (array.size()==0){
            System.out.println("当前学生库中没有信息，输入有误");
        }
        else {
            for (int i = 0; i < array.size(); i++) {
                Student1 id = array.get(i);
                if (id.getSid().equals(sid)) {
                    array.remove(i);
                    System.out.println("删除学生成功");
                    break;
                } else {
                    System.out.println("您输入的学生信息不存在");
                }
            }
        }
    }


    public  static void updateStudent(ArrayList<Student1> array){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要修改学生的学号：");
        String sid = scanner.nextLine();

        System.out.println("请输入学生的新姓名：");
        String name = scanner.nextLine();

        System.out.println("请输入学生的新年龄：");
        String age = scanner.nextLine();

        System.out.println("请输入学生的新地址：");
        String address = scanner.nextLine();

        Student1 s = new Student1();
        s.setSid(sid);
        s.setName(name);
        s.setAge(age);
        s.setAddress(address);

        for (int i=0;i<array.size();i++){
            Student1 student1 = array.get(i);
            if (student1.getSid().equals(sid)){
                array.set(i,s);
                break;
            }
        }

        System.out.println("修改成功");
    }
}
